<template>
  <div class="add">
    <el-card>
      <div>
        <el-icon><Tickets /></el-icon>数据列表
      </div>
      <el-button type="primary" @click="router.push('/pms/addProductCate')"
        >添加</el-button
      >
    </el-card>
  </div>
  <el-table
    border
    table-layout="auto"
    cell-class-name="align-c"
    header-row-class-name="align-c"
    :data="userList"
  >
    <el-table-column label="编号" prop="id" />
    <el-table-column label="分类名称" prop="name" />
    <el-table-column label="级别" prop="nickName">一级</el-table-column>
    <el-table-column label="商品数量" prop="productCount" />
    <el-table-column label="数量单位" prop="productUnit" />
    <el-table-column label="导航栏" prop="navStatus">
      <template #default="{ row }">
        <el-switch
          v-model="row.navStatus"
          :active-value="1"
          :inactive-value="0"
          @change="switchnavStatus(row.id, row.navStatus)"
        />
      </template>
    </el-table-column>
    <el-table-column label="是否显示" prop="showStatus">
      <template #default="{ row }">
        <el-switch
          v-model="row.showStatus"
          :active-value="1"
          :inactive-value="0"
          @change="switchStatus(row.id, row.showStatus)"
        />
      </template>
    </el-table-column>
    <el-table-column label="排序" prop="sort" />

    <el-table-column label="设置">
      <template #default="{ row }">
        <el-button @click="productCate(row.id)" :disabled="isDisable"
          >查看下级</el-button
        >
        <el-button>转移商品</el-button>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button
          type="primary"
          v-permission="'allocRole'"
          @click="router.push(`/pms/addProductCate?id=${row.id}`)"
          >编辑</el-button
        >
        <el-button
          type="danger"
          v-permission="'allocRole'"
          @click="Detele(row.id)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <ElConfigProvider :locale="zhCn">
    <div class="page">
      <el-pagination
        v-model:current-page="form.pageNum"
        v-model:page-size="form.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="CategoryList"
        @current-change="CategoryList"
      />
    </div>
  </ElConfigProvider>
</template>
<script setup lang="ts">
import { Tickets } from "@element-plus/icons-vue";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import {
  ProductCategoryListApi,
  ProductCategoryUpdatenavStatusApi,
  ProductCategoryUpdateshowStatusApi,
  ProductCategoryDeleteApi,
} from "./api/api";
import { useRouter } from "vue-router";
import { reactive, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
const router = useRouter();
const form = reactive<IUserListParams>({
  pageNum: 1,
  pageSize: 5,
});
const userList = ref<ProductCategoryList[]>([]);

// 导航栏
const switchnavStatus = (id: number, navStatus: number) => {
  ProductCategoryUpdatenavStatusApi(id, navStatus)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功");
      } else {
        navStatus = navStatus === 1 ? 0 : 1;
      }
    })
    .catch(() => {
      navStatus = navStatus === 1 ? 0 : 1;
    });
};
// 是否显示
const switchStatus = (id: number, showStatus: number) => {
  ProductCategoryUpdateshowStatusApi(id, showStatus)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功");
      } else {
        showStatus = showStatus === 1 ? 0 : 1;
      }
    })
    .catch(() => {
      showStatus = showStatus === 1 ? 0 : 1;
    });
};
// 删除
const Detele = (id: number) => {
  ElMessageBox.confirm("确定要删除用户吗？", "删除用户", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
  })
    .then(() => {
      // 发请求
      ProductCategoryDeleteApi(id).then((res) => {
        if (res.code === 200) {
          // 提示
          ElMessage.success("删除成功");
        }
      });
    })
    .catch(() => {
      console.log("点击取消");
    });
};
const total = ref(0);
// 表格
const CategoryList = () => {
  ProductCategoryListApi(0, form).then((res) => {
    if (res.code === 200) {
      userList.value = res.data.list;
      total.value = res.data.total;
    }
  });
};
// 查看下一级
let isDisable = ref(false);
const productCate = (id: number) => {
  ProductCategoryListApi(id, form).then((res) => {
    if (res.code === 200) {
      userList.value = res.data.list;
      total.value = res.data.total;
      isDisable.value = true;
    }
  });
};
CategoryList();
</script>
<style lang="less" scoped>
.add {
  margin: 20px 0;
  :deep(.el-card__body) {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
}
.page {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
:deep(.cell) {
  text-align: center;
}
</style>
